package bg.cinema_system.beans;

import java.sql.SQLException;
import java.text.ParseException;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.primefaces.context.RequestContext;
import org.primefaces.util.Constants;

import bg.cinema_system.pojo.Movie;
import bg.cinema_system.pojo.Projection;
import bg.cinema_system.service.MovieService;
import bg.cinema_system.service.ProjectionService;

@ManagedBean
@RequestScoped
public class StaffBean extends BaseBean{

	private static final long serialVersionUID = 4661801409608642301L;
	
	private String warninMssageProjection;
	private String succesMessageProjection;
	private String warninMssageMovie;
	private String succesMessageMovie;

	private Movie moviePojo;
	private Projection projection;

	public StaffBean() {
		moviePojo = new Movie();
		projection = new Projection();
	}


	public String getWarninMssageProjection() {
		return warninMssageProjection;
	}
	public void setWarninMssageProjection(String warninMssageProjection) {
		this.warninMssageProjection = warninMssageProjection;
	}

	public String getSuccesMessageProjection() {
		return succesMessageProjection;
	}

	public void setSuccesMessageProjection(String succesMessageProjection) {
		this.succesMessageProjection = succesMessageProjection;
	}

	public String getWarninMssageMovie() {
		return warninMssageMovie;
	}

	public void setWarninMssageMovie(String warninMssageMovie) {
		this.warninMssageMovie = warninMssageMovie;
	}

	public String getSuccesMessageMovie() {
		return succesMessageMovie;
	}

	public void setSuccesMessageMovie(String succesMessageMovie) {
		this.succesMessageMovie = succesMessageMovie;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public Movie getMoviePojo() {
		return moviePojo;
	}

	public void setMoviePojo(Movie moviePojo) {
		this.moviePojo = moviePojo;
	}

	public Projection getProjection() {
		return projection;
	}

	public void setProjection(Projection projection) {
		this.projection = projection;
	}
	
	public List<String> getListHallNumbers(){
		List<String> hallNumbers = null;
		try {
			hallNumbers = ProjectionService.getHallNumbers();
		} catch (SQLException e) {
//			warninMssage = "Can not get the hall numbers!";
			setMessages(null, null, "Филмът не е добавен. Опитайте отново!", null);
			e.printStackTrace();
		}
		return hallNumbers;
	}
	
	public List<String> getListMovieNames(){
		List<String> movieNames = null;
		try {
			movieNames = MovieService.getAllMovieNames();
		} catch (SQLException e) {
//			warninMssage = "Can not get the movie names!";
			setMessages("Филмът не е добавен. Опитайте отново!", null, null, null);
			e.printStackTrace();
		}
		return movieNames;
	}
	public void addMovie(ActionEvent event) {
		boolean isMessgaDisplay = true;
		try {
			MovieService.insertMovie(moviePojo);
		} catch (SQLException e) {
			isMessgaDisplay = false;
			setMessages("Филмът не е добавен. Опитайте отново!", null, null, null);
			e.printStackTrace();
		} catch (ParseException e) {
			isMessgaDisplay = false;
			setMessages("Филмът не е добавен. Опитайте отново!", null, null, null);
			e.printStackTrace();
		}
		if (isMessgaDisplay) {
			setMessages(null, "Успешно добавен филм.", null, null);
		}
		moviePojo = new Movie();
//		getListMovieNames();
//		getRequestContext().execute("dialogMessage.show()");
	}

	public void addProjection(ActionEvent event) {
		boolean isMessgaDisplay = true;
		try {
			ProjectionService.insertProjection(projection);
		} catch (SQLException e) {
			isMessgaDisplay = false;
			setMessages(null, null, "Прожекцията не е добавен. Опитайте отново!", null);
			e.printStackTrace();
		} catch (ParseException e) {
			isMessgaDisplay = false;
			setMessages(null, null, "Прожекцията не е добавен. Опитайте отново!", null);
			e.printStackTrace();
		}
		if (isMessgaDisplay) {
			setMessages(null, null, null, "Успешно добавена прожекция.");
		}
//		RequestContext req = (RequestContext)FacesContext.getCurrentInstance().getAttributes().get(Constants.REQUEST_CONTEXT_ATTR);
		projection = new Projection();
//		req.execute("dialogMessage.show()");
		
	}
	
	private void setMessages(String warninMssageMovie, String succesMessageMovie, String warninMssageProjection, String succesMessageProjection){
		this.warninMssageMovie = warninMssageMovie;
		this.succesMessageMovie = succesMessageMovie;
		this.warninMssageProjection = warninMssageProjection;
		this.succesMessageProjection = succesMessageProjection;
	}
}
